<?php
namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class AuthRule extends Base
{
    use SoftDelete;
    // 设置当前模型的数据库连接
    /*
    * 获取列表信息
    */

    public function getListAll() {
        //获取一级权限
        $data = $this->where(array('pid' => 0))->order('sort desc,id asc')->select();
        $data = $data->toArray();
        $redata = array();
        if (!empty($data)) {
            $idData = array();
            foreach ($data as $value) {
                $idData[] = $value['id'];
            }
            //获取二级权限
            $where_second[] = ['pid','in',$idData];
            $where_second[] = ['type','eq',2];
            $list = $this->where($where_second)->order('sort desc,id asc')->select();
            $list = $list->toArray();
            $idData1 = array();
            foreach ($list as $value) {
                $idData1[] = $value['id'];
            }
            //获取三级权限
            $where_third[] = ['pid','in',$idData1];
            $where_third[] = ['type','eq',3];
            $list_next = $this->where($where_third)->order('sort desc,id asc')->select();
            $list_next = $list_next->toArray();
            foreach ($data as $value) {
                $temp = array();
                $temp['id'] = $value['id'];
                $temp['pid'] = $value['pid'];
                $temp['title'] = $value['title'];
                $temp['rule_val'] = $value['rule_val'];
                $temp['rule_url'] = $value['rule_url'];
                $temp['icon'] = $value['icon'];
                $temp['type'] = $value['type'];
                $temp['sort'] = $value['sort'];
                $temp['list'] = array();
                //写入下一级信息
                foreach ($list as $val) {
                    if ($val['pid'] == $value['id']) {
                        $temp['list'][] = $val;
                    }
                }
                //写入下二级信息
                if (!empty($temp['list'])) {
                    foreach ($temp['list'] as $key => $val) {
                        $val['next'] = array();
                        $next = array();
                        foreach ($list_next as $v) {
                            if ($v['pid'] == $val['id']) {
                                $next[] = $v;
                            }
                        }
                        $val['next'] = $next;
                        $temp['list'][$key] = $val;
                    }
                }
                $redata[] = $temp;
            }
        }
        $newData = [];
        foreach ($redata as $key=>$value){
            $new = $value;
            unset($new['list']);
            $newData[] = $new;
            foreach ($value['list'] as $val){
                $new = $val;
                unset($new['next']);
                $newData[] = $new;
                foreach ($val['next'] as $v){
                    $newData[] = $v;
                }
            }
        }
        foreach ($newData as $key=>$value){
            switch ($value['type'])
            {
                case 1:
                    $newData[$key]['show_title'] =$value['title'];
                    $newData[$key]['type_name'] = "权限组";
                        break;
                case 2:
                    $newData[$key]['show_title'] = "|-".$value['title'];
                    $newData[$key]['type_name'] = "一级权限";
                        break;
                case 3:
                    $newData[$key]['show_title'] = "|-----".$value['title'];
                    $newData[$key]['type_name'] = "二级权限";
                        break;
            }
        }
        return $newData;
    }

    /*
     * 获取等级数据(用于角色授权页面)
     */
    public function getLevelData() {
        $data = $this->where('uid','eq',$this->mid)->order('pid asc, sort desc,id asc')->select();
        if (empty($data)) {
            return $data;
        }
        $ret = [];
        $thridarr = [];
        foreach ($data as $val) {
            if ($val['type'] == 3) {
                $temp = ['id' => $val->id, 'title' => $val->title, 'pid' => $val->pid, 'rule_val' => $val->rule_val];
                if (!isset($thridarr[$val->pid])) {
                    $thridarr[$val->pid][] = $temp;
                } else {
                    array_push($thridarr[$val->pid], $temp);
                }
            }
            if ($val->pid == 0) {
                $ret[$val->id] = ['id' => $val->id, 'title' => $val->title, 'pid' => $val->pid, 'rule_val' => $val->rule_val];
            } elseif (isset($ret[$val->pid])) {
                $ret[$val->pid]['children'][$val->id] = ['id' => $val->id, 'title' => $val->title, 'pid' => $val->pid, 'rule_val' => $val->rule_val];
            }

        }
        foreach ($ret as $key => $value) {
            if (!empty($value['children'])) {
                foreach ($value['children'] as $k => $val) {
                    foreach ($thridarr as $kk => $vv) {
                        if ($k == $kk) {
                            $val['next'] = $vv;
                        }
                    }
                    $value['children'][$k] = $val;
                }
            }

            $ret[$key] = $value;
        }
        return $ret;
    }

}